12. Command Line Tutorial, Part II — Creating and Removing Files
Command Line Tutorial, Part II — Creating and Removing Directories and Files
In the last lesson, we learned to use pwd, ls, open, and cd to move around our computer’s file tree. The next logical step is to learn about creating and removing files and directories. Here, we’ll focus on the creation of files and folders using two new commands: touch and mkdir. To demonstrate how to use these commands, we'll create a file structure to represent a very simple animal hierarchy.
Creating Directories and Files
First, let's take a moment to think about how you would create a folder using the GUI. On a Mac, folders are created by open the Finder and then selecting from the menu bar: File → New Folder.
Making a new folder through a GUI.
Let’s consider how we can achieve the same result using the command line.
The mkdir command
The mkdir command (often pronounced make-dir or “M”-“K”-“dir”) is used to make a new directory with the name that we provide. For example, if you’re in the Desktop directory and want to create an animals directory in that location, you'd type the following command:
~/Desktop $ mkdir animals
Using the mkdir command.
If you wanted to then create a rodents directory inside of animals, you could do either of the following (starting from the Desktop):
- use
cdto move intoanimals, and then type:mkdir rodents, or - simply run the command
mkdir animals/rodentsfrom your current directory.
These are not the only two approaches, but they are perhaps the most sensible.
Creating multiple directories
You can also use mkdir to create multiple directories at once. To do so, you provide a list of directory names separated by spaces. For example, to create three directories with names of marsupials, cloven_hoofed_animals, and carnivores inside the animals directory, you can use the following command:
~/Desktop/animals $ mkdir marsupials cloven_hoofed_animals carnivores
Note: You should avoid using spaces in directory and file names (hence the underscores in cloven_hoofed_animals). If this is unavoidable — perhaps if you encounter a previously created file or directory name with a space in it — you can insert what’s called an escape (\) character before the space to tell the shell how to properly interpret the spaces in that file name, e.g. mkdir cloven\ hoofed\ animals.
The touch command
Creating a file in your working directory
Until this point, you’ve most likely created and saved files on your computer by using a particular application — for example, creating a .docx file in Microsoft Word, a .pdf file in Adobe Acrobat, or an .html file in a text editor. At the command line, you can create a file in your working directory with a single command called touch. The touch command creates a new (empty) file with the name and file extension that you provide. For example, if you’re in the Desktop directory and want to create a file named first_file.txt in that location, you can type the following:
~/Desktop $ touch first_file.txt
The inclusion of the file extension (.txt in this case) is crucial because it sets the file type. Running the above command and then listing the contents of the Desktop directory confirms that a text file with the name first_file.txt was indeed created.
Using the touch command to create a file in the working directory.
Creating a file in a directory other than the working directory
You can also use the touch command to create a file that resides in a directory other than your working directory. To do this, you must prepend to your file name the path leading to your desired destination. For instance, given the following file structure, you can create from your Desktop working directory a text file named capybara.txt that resides several layers deeper than your working directory.
Example file structure:
Home(or~) directory →Desktopdirectory →animalsdirectory →rodentsdirectory →capybara.txtfile
To do so, you’d execute the following command:
~/Desktop $ touch animals/rodents/capybara.txt
Listing the contents of the rodents directory (at ~/Desktop/animals/rodents) will confirm the presence of a new text file named capybara.txt, as shown below.
Using the touch command to create a file in a directory other than the working directory.
Creating multiple files
You can create multiple files with the touch command by providing multiple file names (including any appropriate prepended paths), separated by spaces. For example, the code below will create two files:
- one file named
kangaroo.txtresiding at:~→Desktop→animals→marsupials, and - another file named
giraffe.txtresiding at:~→Desktop→animals→cloven_hoofed_animals
~/Desktop $ touch animals/marsupials/kangaroo.txt animals/cloven_hoofed_animals/giraffe.txt
Creating different file types
Lastly, it’s important to note that you aren’t restricted to creating only text files with the touch command; you can create any file type you please. For instance, the following command will create (in the Desktop working directory) a Python file with the name test.py:
~/Desktop $ touch test.py
Removing Directories and Files
In this section, you’ll learn how to use the rm command to permanently delete directories and files. We’ll start off by deleting files because it can be a bit more straightforward than deleting directories.
Removing files
The rm command
You can use the rm (or “remove”) command to delete specified files. Please note that THIS DELETION IS PERMANENT — it cannot be reversed. Only use rm if you are sure you want to delete something.
Let’s consider an example. If you want to remove a file named first_file.txt that resides in the Desktop directory, you can run the following command:
~/Desktop $ rm first_file.txt
Using rm to remove a file.
Deleting multiple files is accomplished by providing a list of file names, separated by spaces. For instance, the image below demonstrates a scenario in which we use the touch command to create three text files in our Desktop directory — apples.txt, carrots.txt, and fruits.txt — and then remove (or permanently delete) those files using the rm command. The code to accomplish this deletion is as follows:
$ rm apples.txt carrots.txt fruits.txt`
Caption: Using rm to remove multiple files.
Removing directories
You just learned how to remove files using the rm command. Here, we’ll consider two ways to remove a directory:
- using the
rmdircommand, and - using the
rm -rcommand (NOTE: EXERCISE CAUTION).
The rmdir command
The rmdir command can be used to delete directories, but it’s important to note that it can only remove completely empty directories. For example, you can use mkdir to create a directory called cats inside the Desktop directory. Because cats is just an empty directory (nothing has been added to it), you can successfully use the rmdir command to remove the cats directory, as shown in the image below.
Using rmdir to remove an empty directory.
However, if you try to run rmdir on a directory that is not empty, you’ll receive an error message. For example, if you create a cats directory and then add files to it (e.g. munchkin.txt and tabby.rb), you won’t be able to use rmdir to remove the cats directory, as shown in the image below.
Error message — rmdir cannot remove any directory with contents.
A potential solution would involve first emptying the directory’s contents one file at a time (using the rm command you learned above) and then using rmdir to remove the emptied directory, but this can be inefficient if a directory contains many files. The next section reveals a faster approach.
The rm -r command
As you saw above, using rmdir to remove directories was somewhat limited because it only could remove empty directories. Here, you’ll learn how to delete directories — including those with contents — by executing a slightly different version of the rm command you used earlier to delete files.
To remove a directory, you can add something called a flag or option to the rm command. Flags provide additional execution instructions. They are included after the command name but before the argument. Flags are indicated by letters following a dash (-) symbol. The complete command we’ll use in this section is rm -r, where r stands for recursive.
CAUTION: Before you proceed, you should know that recursive deletion means deleting all contents of a directory — that includes its files, its sub-directories, any files within those sub-directories, and so on. Furthermore, deletion with rm -r is a permanent action. You can easily and irreversibly delete great numbers of files with this one command.
Let’s consider an example. If you want to remove a non-empty directory named cats that lives in your Desktop directory, you could use the following command:
~/Desktop $ rm -r cats
If you’d like to be asked to confirm the deletion of individual items contained in a directory, you can add the interactive (-i) flag to your command, either by combining it with the -r flag (e.g. rm -ri) or by adding it separately (e.g. rm -r -i). For instance, if the cats directory happened to contain two files (file_1.txt and file_2.txt) and four sub-directories (calico, persian, scottish_fold, and siamese), you could use the following command to delete the cats directory after confirming the deletion of its several contents:
~/Desktop $ rm -ri cats
To proceed through these confirmation messages, you can simply type yes or y. The image below depicts this interaction.
Using rm -ri to remove a non-empty directory only after confirming deletion of its contents.